﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Ultima.Xna.Data;
using Ultima.Xna.Extensions;
using Ultima.Xna.Input;
using System.Xml;

namespace Ultima.Xna.Graphics.UI
{
    public class GumpLabel : UINode, IMouseAttachable, IGumpLabel
    {
        private int _hue = 1;
        private int _fontId = 9;
        private string _text;

        public int FontId
        {
            get { return _fontId; }
            set { _fontId = (int)MathHelper.Clamp(value, 0, 10); }
        }

        public ASCIIFont Font
        {
            get { return ASCIIFont.GetFixed(_fontId); }

        }

        public int Hue
        {
            get { return _hue; }
            set { _hue = value; }
        }

        public override Vector2 Size
        {
            get  { return new Vector2(Font.GetWidth(_text), (Font.Height * (_text.CountChar('\n') + 1))); }
            set { base.Size = value; }
        }

        public virtual string Text
        {
            get { return Utility.WrapASCIIText(_fontId, _text, 99999); }
            set { _text = value;  }
        }

        public virtual bool Moveable
        {
            get
            {
                bool moveable = false;

                if (_parent != null && _parent is IMouseAttachable)
                {
                    moveable = ((IMouseAttachable)_parent).Moveable;
                }

                return moveable;
            }
        }

        public GumpLabel(Game game, IUIContainerNode parent)
            : base(game, parent)
        {

        }

        public override void HandleClick(MouseButtons button, bool down)
        {
            if (_defaultBehaviour)
            {
                if (down)
                {
                    _manager.AttachMouse(this);
                }
                else
                {
                    _manager.DettachMouse();
                }
            }
            else
            {
                base.HandleClick(button, down);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            PresentationParameters pp = _graphicsDevice.PresentationParameters;

            _spriteBatch.End();

            if (_spriteBatch.Effect != null)
            {
                _spriteBatch.Effect.Parameters["HueTexture"].SetValue(Hues.Texture);
                _spriteBatch.Effect.Parameters["Hue"].SetValue(new Vector2(_hue - 1, 0));
            }

            _spriteBatch.BeginLast();

            RectangleF renderBounds;
            CalculateRenderBounds(out renderBounds);

            ASCIIFont font = Font;
            string text = Text;
            Vector2 position = new Vector2(renderBounds.X, renderBounds.Y);
            Vector2 fontSize = new Vector2(font.GetWidth(text), (font.Height * (text.CountChar('\n') + 1)));

            float dx = 0;
            float height = font.Height;

            string[] split = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < split.Length; i++)
            {
                dx = 0;

                for (int x = 0; x < split[i].Length; x++)
                {
                    _spriteBatch.Draw(font.GetTexture(split[i][x]), position + new Vector2(dx, i * font.Height), Color.White);
                    dx += font.GetWidth(split[i][x].ToString());
                }
            }

            if (_spriteBatch.Effect != null)
            {
                _spriteBatch.Effect.Parameters["Hue"].SetValue(new Vector2(-1, 0));
            }

            _spriteBatch.End();
            _spriteBatch.BeginLast();

            base.Draw(gameTime);       
        }
    }
}
